/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 處理收到由客戶端傳來選擇目標的封包
 */
public class C_SelectTarget extends ClientBasePacket {

	private static final String C_SELECT_TARGET = "[C] C_SelectTarget";
	private static final Logger _log = Logger.getLogger(C_SelectTarget.class.getName());
	@Override
	public String getType() {
		return C_SELECT_TARGET;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final int petId = this.readD();
			this.readC();
			final int targetId = this.readD();

			final L1PetInstance pet = (L1PetInstance) L1World.getInstance().findObject(petId);
			final L1Character target = (L1Character) L1World.getInstance().findObject(targetId);

			if (pet != null && target != null) {
				if (target instanceof L1PcInstance) {
					final L1PcInstance pc = (L1PcInstance) target;
					//TODO 修正寵物指定攻擊指令於安全區域使用時判斷byBAO
					if (pc.checkNonPvP(pc, pet) || pc.getZoneType() == 1) {
						//TODO 修正寵物指定攻擊指令於安全區域使用時判斷byBAO
						return;
					}
				}
				pet.setMasterTarget(target);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
